<?php

namespace common\models\c2\entity;

use common\helpers\Tool;
use Yii;
use yii\helpers\Json;

/**
 * This is the model class for table "{{%unit}}".
 *
 * @property integer $id
 * @property integer $id2
 * @property integer $community_id
 * @property integer $building_number_id
 * @property string $name
 * @property integer $status
 * @property string $remarks
 */
class Unit extends \cza\base\models\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%unit}}';
    }

    public function behaviors()
    {
        return []; // TODO: Change the autogenerated stub
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['community_id', 'building_number_id', 'status', 'id2'], 'integer'],
            [['name', 'remarks'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app.c2', 'ID'),
            'community_id' => Yii::t('app.c2', 'Community ID'),
            'building_number_id' => Yii::t('app.c2', 'Building Number ID'),
            'name' => Yii::t('app.c2', 'Name'),
            'status' => Yii::t('app.c2', 'Status'),
            'remarks' => Yii::t('app.c2', 'Remarks'),
        ];
    }

    /**
     * @inheritdoc
     * @return \common\models\c2\query\UnitQuery the active query used by this AR class.
     */
    public static function find()
    {
        return new \common\models\c2\query\UnitQuery(get_called_class());
    }
    
    /**
    * setup default values
    **/
    public function loadDefaultValues($skipIfSet = true) {
        parent::loadDefaultValues($skipIfSet);
    }

    public function beforeSave($insert)
    {

        $data = [
            // 'id' => $this->id2,
            'community_id' => $this->community_id,
            'building_number_id' => $this->building_number_id,
            'name' => $this->name,
            'status' => $this->status,
            'remarks' => $this->remarks,
        ];

        if ($this->isNewRecord) {
            $url = \Yii::$app->params['api_url'] . '/api/Building/SetAddUnit?supplier_id=' . Yii::$app->params['supplier_id'];
        } else {
            $data['id'] = $this->id2;
            $url = \Yii::$app->params['api_url'] . '/api/Building/SetUpdateUnit?supplier_id=' . Yii::$app->params['supplier_id'];
        }

        // 同步到第三方平台
        $data = Json::encode($data);
        $result = Tool::curlPost($url, $data);
        Yii::info($result);

        $result = json_decode($result, true);
        if ($result['Status'] && $result['Status'] == 1) {
            if ($this->isNewRecord) {
                $this->id2 = $result['Ext'];
            }
            return parent::beforeSave($insert); // TODO: Change the autogenerated stub
        }

        Yii::$app->session->setFlash($this->getMessageName(), $result['Msg']);
        return false;
    }


    public static function getHashMap($keyField, $valField, $condition = '')
    {
        if (!Yii::$app->user->can('P_SYSTEM')) {
            $condition = ['community_id' => Yii::$app->user->getCommunityID()];
        }
        return parent::getHashMap($keyField, $valField, $condition); // TODO: Change the autogenerated stub
    }


}
